﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace jnxceq.DAL
{
	/// <summary>
	/// 数据访问类:t_cbdStatus
	/// </summary>
	public partial class t_cbdStatus
	{
		public t_cbdStatus()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("keyid", "t_cbdStatus"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int keyid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_cbdStatus");
			strSql.Append(" where keyid=@keyid");
			SqlParameter[] parameters = {
					new SqlParameter("@keyid", SqlDbType.Int,4)
			};
			parameters[0].Value = keyid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnxceq.Model.t_cbdStatus model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_cbdStatus(");
			strSql.Append("devID,devInfo,sta_ch0,sta_ch1,sta_ch2,sta_ch3,sta_ch4,sta_ch5,sta_ch6,sta_ch7,updateTime,Note)");
			strSql.Append(" values (");
			strSql.Append("@devID,@devInfo,@sta_ch0,@sta_ch1,@sta_ch2,@sta_ch3,@sta_ch4,@sta_ch5,@sta_ch6,@sta_ch7,@updateTime,@Note)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@devID", SqlDbType.Int,4),
					new SqlParameter("@devInfo", SqlDbType.VarChar,200),
					new SqlParameter("@sta_ch0", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch1", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch2", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch3", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch4", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch5", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch6", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch7", SqlDbType.VarChar,50),
					new SqlParameter("@updateTime", SqlDbType.DateTime),
					new SqlParameter("@Note", SqlDbType.VarChar,200)};
			parameters[0].Value = model.devID;
			parameters[1].Value = model.devInfo;
			parameters[2].Value = model.sta_ch0;
			parameters[3].Value = model.sta_ch1;
			parameters[4].Value = model.sta_ch2;
			parameters[5].Value = model.sta_ch3;
			parameters[6].Value = model.sta_ch4;
			parameters[7].Value = model.sta_ch5;
			parameters[8].Value = model.sta_ch6;
			parameters[9].Value = model.sta_ch7;
			parameters[10].Value = model.updateTime;
			parameters[11].Value = model.Note;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnxceq.Model.t_cbdStatus model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_cbdStatus set ");
			strSql.Append("devID=@devID,");
			strSql.Append("devInfo=@devInfo,");
			strSql.Append("sta_ch0=@sta_ch0,");
			strSql.Append("sta_ch1=@sta_ch1,");
			strSql.Append("sta_ch2=@sta_ch2,");
			strSql.Append("sta_ch3=@sta_ch3,");
			strSql.Append("sta_ch4=@sta_ch4,");
			strSql.Append("sta_ch5=@sta_ch5,");
			strSql.Append("sta_ch6=@sta_ch6,");
			strSql.Append("sta_ch7=@sta_ch7,");
			strSql.Append("updateTime=@updateTime,");
			strSql.Append("Note=@Note");
			strSql.Append(" where keyid=@keyid");
			SqlParameter[] parameters = {
					new SqlParameter("@devID", SqlDbType.Int,4),
					new SqlParameter("@devInfo", SqlDbType.VarChar,200),
					new SqlParameter("@sta_ch0", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch1", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch2", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch3", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch4", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch5", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch6", SqlDbType.VarChar,50),
					new SqlParameter("@sta_ch7", SqlDbType.VarChar,50),
					new SqlParameter("@updateTime", SqlDbType.DateTime),
					new SqlParameter("@Note", SqlDbType.VarChar,200),
					new SqlParameter("@keyid", SqlDbType.Int,4)};
			parameters[0].Value = model.devID;
			parameters[1].Value = model.devInfo;
			parameters[2].Value = model.sta_ch0;
			parameters[3].Value = model.sta_ch1;
			parameters[4].Value = model.sta_ch2;
			parameters[5].Value = model.sta_ch3;
			parameters[6].Value = model.sta_ch4;
			parameters[7].Value = model.sta_ch5;
			parameters[8].Value = model.sta_ch6;
			parameters[9].Value = model.sta_ch7;
			parameters[10].Value = model.updateTime;
			parameters[11].Value = model.Note;
			parameters[12].Value = model.keyid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int keyid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_cbdStatus ");
			strSql.Append(" where keyid=@keyid");
			SqlParameter[] parameters = {
					new SqlParameter("@keyid", SqlDbType.Int,4)
			};
			parameters[0].Value = keyid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string keyidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_cbdStatus ");
			strSql.Append(" where keyid in ("+keyidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxceq.Model.t_cbdStatus GetModel(int keyid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 keyid,devID,devInfo,sta_ch0,sta_ch1,sta_ch2,sta_ch3,sta_ch4,sta_ch5,sta_ch6,sta_ch7,updateTime,Note from t_cbdStatus ");
			strSql.Append(" where keyid=@keyid");
			SqlParameter[] parameters = {
					new SqlParameter("@keyid", SqlDbType.Int,4)
			};
			parameters[0].Value = keyid;

			jnxceq.Model.t_cbdStatus model=new jnxceq.Model.t_cbdStatus();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxceq.Model.t_cbdStatus DataRowToModel(DataRow row)
		{
			jnxceq.Model.t_cbdStatus model=new jnxceq.Model.t_cbdStatus();
			if (row != null)
			{
				if(row["keyid"]!=null && row["keyid"].ToString()!="")
				{
					model.keyid=int.Parse(row["keyid"].ToString());
				}
				if(row["devID"]!=null && row["devID"].ToString()!="")
				{
					model.devID=int.Parse(row["devID"].ToString());
				}
				if(row["devInfo"]!=null)
				{
					model.devInfo=row["devInfo"].ToString();
				}
				if(row["sta_ch0"]!=null)
				{
					model.sta_ch0=row["sta_ch0"].ToString();
				}
				if(row["sta_ch1"]!=null)
				{
					model.sta_ch1=row["sta_ch1"].ToString();
				}
				if(row["sta_ch2"]!=null)
				{
					model.sta_ch2=row["sta_ch2"].ToString();
				}
				if(row["sta_ch3"]!=null)
				{
					model.sta_ch3=row["sta_ch3"].ToString();
				}
				if(row["sta_ch4"]!=null)
				{
					model.sta_ch4=row["sta_ch4"].ToString();
				}
				if(row["sta_ch5"]!=null)
				{
					model.sta_ch5=row["sta_ch5"].ToString();
				}
				if(row["sta_ch6"]!=null)
				{
					model.sta_ch6=row["sta_ch6"].ToString();
				}
				if(row["sta_ch7"]!=null)
				{
					model.sta_ch7=row["sta_ch7"].ToString();
				}
				if(row["updateTime"]!=null && row["updateTime"].ToString()!="")
				{
					model.updateTime=DateTime.Parse(row["updateTime"].ToString());
				}
				if(row["Note"]!=null)
				{
					model.Note=row["Note"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select keyid,devID,devInfo,sta_ch0,sta_ch1,sta_ch2,sta_ch3,sta_ch4,sta_ch5,sta_ch6,sta_ch7,updateTime,Note ");
			strSql.Append(" FROM t_cbdStatus ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" keyid,devID,devInfo,sta_ch0,sta_ch1,sta_ch2,sta_ch3,sta_ch4,sta_ch5,sta_ch6,sta_ch7,updateTime,Note ");
			strSql.Append(" FROM t_cbdStatus ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_cbdStatus ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.keyid desc");
			}
			strSql.Append(")AS Row, T.*  from t_cbdStatus T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_cbdStatus";
			parameters[1].Value = "keyid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

